// pages/list/list.js
const shopcartAnimate = require('../../utils/shopcartAnimate.js')
const app = getApp()
const fetch = app.fetch
const categoryposition = []
Page({
  data: {
    foodList: [],
    promotion: {},
    activeindex: 0,
    tapindex: 0,
    cartPrice: 0,
    cartList: [],
    showCart: false,
  },
  disableNextScroll: false,
  shopcartAnimate: null,
  onLoad: function () {
    wx.showLoading({
      title: "努力加载中"
    }),
      fetch('/food/list').then(data => {
        wx.hideLoading()
        this.setData({
          foodList: data.list,
          promotion: data.promotion[0]
        }, () => {
          var query = wx.createSelectorQuery()
          var top = 0
          var height = 0
          query.select('.food').boundingClientRect(rect => {
            top = rect.top
            height = rect.height
          })
          query.selectAll('.food-category').boundingClientRect(res => {
            res.forEach(rect => {
              categoryposition.push(rect.top - top - height / 3)
            })
          })
          query.exec()
        })
      }, () => {
        this.onLoad()
      })
    this.shopcartAnimate = shopcartAnimate('.operate-shopcart-icon', this)
  },
  tapCategory: function (e) {
    this.disableNextScroll = true
    var index = e.currentTarget.dataset.index
    this.setData({
      activeindex: index,
      tapindex: index
    })
  },
  onFoodScroll: function (e) {
    if (this.disableNextScroll) {
      this.disableNextScroll = false
      return
    }
    var scrollTop = e.detail.scrollTop
    var activeindex = e
    categoryposition.forEach((item, i) => {
      if (scrollTop >= item) {
        activeindex = i
      }
    })
    if (activeindex !== this.data.activeindex) {
      this.setData({ activeindex })
    }
  },
  addToCart: function (e) {
    let index = e.currentTarget.dataset.index
    let category_index = e.currentTarget.dataset.category_index
    let food = this.data.foodList[category_index].food[index]
    let cartList = this.data.cartList

    let cartFood = cartList.find(item => item.id == food.id)
    if (cartFood) {
      ++cartFood.number
    } else {
      cartList.push({
        id: food.id,
        name: food.name,
        price: parseFloat(food.price),
        number: 1
      });
    }

    this.setData({
      cartList,
      cartPrice: this.data.cartPrice + parseFloat(food.price) 
    })
    this.shopcartAnimate.start(e)
  },
  showCartList: function () {
    if (this.data.cartList.length > 0) {
      this.setData({
        showCart: !this.data.showCart
      })
    }
  },
  cartNumberAdd: function (e) {
    var id = e.currentTarget.dataset.id
    var cartList = this.data.cartList
    ++cartList[id].number
    this.setData({
      cartList: cartList,
      cartPrice: this.data.cartPrice + cartList[id].price
    })
  },
  cartNumberDec: function (e) {
    var id = e.currentTarget.dataset.id
    var cartList = this.data.cartList

    if (cartList[id]) {
      var price = cartList[id].price
      if (cartList[id].number > 1) {
        --cartList[id].number
      } else {
        cartList.splice(id)
      }
      this.setData({
        cartList: cartList,
        cartPrice: this.data.cartPrice - price
      })
      if (this.data.cartList.length <= 0) {
        this.setData({
          showCart: false
        })
      }
    }
  },
  cartClear: function () {
    this.setData({
      cartList: [],
      cartNunber: 9,
      cartPrice: 0,
      showCart: false
    })
  },
  order: function () {
    if (this.data.cartList.length === 0) {
      return
    }
    wx.showLoading({
      title: "正在生成订单"
    })
    fetch('/food/order', {
      order: this.data.cartList
    }, 'POST').then(data => {
      wx.navigateTo({
        url: '/pages/order/checkout/checkout?order_id=' + data.order_id,
      })
    }, () => {
      this.order()
    })
  }
})



